define(["utils"],function(utils){
    function pageLogic(config){
      this.pageview = config.pageview;

      var _this = this;
      this.pageview.pageManager.bindOnce("userloaded",function(){
        _this._loadData(1);
      });

    }
    pageLogic.prototype = {
      repeat_init:function(sender){
        this.repeat = sender;
      },
      subComponent_click:function(sender,params){
        this.pageview.go("index/template/designer");
      },
      repeat_itemclick:function(sender,params){
        this.pageview.go("index/template/designer",{id:sender.datasource.id,status:sender.datasource.dataStatus});
      },
      _loadData:function(index){
        var _this = this;
        var totalPages;
        this.pageview.showSubmiting({
            text: "正在加载...",
            errorText:"加载失败...",
            successText:null
        });
        this.pageview.ajax({
          url: "/template/list",
          type: "GET",
          data: {
            pageNum:index,
            pageSize:19
          },
          success: function (data) {
            if (data.code === 0) {
              totalPages = data.data.pages;
              _this.pageview.delegate("repeat",function(target){
                target.bindData(data.data.list);
              });
              _this.pageview.delegate("paginator",function(target){

                target.go(index,totalPages);
              });
            }
            _this.pageview.hideSubmiting(data.code === 0);
          },
          error: function () {
            _this.pageview.hideSubmiting(false);
          }
        });

        // this.pageview.delegate("repeat",function(target){
        //   target.bindData([{
        //     title:"体验部专用",
        //     content:"用户体验部的模版22吧啦吧啦吧啦",
        //     dataStatus: 0,
        //     createTime: "2016-08-19 12:30"
        //   },{
        //     title:"开发部专用",
        //     content:"用户体验部的模版22吧啦吧啦吧啦",
        //     dataStatus: 1,
        //     createTime: "2016-08-19 12:30"
        //   },{
        //     title:"KFC专用",
        //     content:"用户体验部的模版22吧啦吧啦吧啦",
        //     dataStatus: 1,
        //     createTime: "2016-08-19 12:30"
        //   },{
        //     title:"麦当劳专用",
        //     content:"用户体验部的模版22吧啦吧啦吧啦",
        //     dataStatus: 1,
        //     createTime: "2016-08-19 12:30"
        //   },{
        //     title:"周黑鸭专用",
        //     content:"用户体验部的模版22吧啦吧啦吧啦",
        //     dataStatus: 0,
        //     createTime: "2016-08-19 12:30"
        //   },{
        //     title:"产品运营专用",
        //     content:"用户体验部的模版22吧啦吧啦吧啦",
        //     dataStatus: 0,
        //     createTime: "2016-08-19 12:30"
        //   },{
        //     title:"老板专用",
        //     content:"用户体验部的模版22吧啦吧啦吧啦",
        //     dataStatus: 1,
        //     createTime: "2016-08-19 12:30"
        //   }]);
        // });

      },
      repeat_item_wrap_init:function (sender,params) {
        if(sender.datasource.dataStatus === 0){
            //停用
          sender.rowInstance.$el.addClass("stop-item");
          sender.rowInstance.$el.removeClass("using-item");
        }
        else{
            //启用
          sender.rowInstance.$el.addClass("using-item");
          sender.rowInstance.$el.removeClass("stop-item");
        }
      },
      repeat_item_time_init:function(sender,params){
        var timestamp = sender.datasource.createTime;
        var dateInfo = utils.timestampToDateInfo(timestamp);
        var timeStr;
        timeStr = "更新于" + dateInfo.year + "-" + dateInfo.monthStr + "-" + dateInfo.dayStr + " " + dateInfo.hourStr +":"+ dateInfo.minStr;
        sender.config.text = timeStr;
      },
      delete_icon_click:function(sender,params){
          //删除
          this.pageview.showDialog("del_dialog");
          this.deleteObj = sender;
      },
      del_dialog_cancelbtn_click: function () {
        this.pageview.hideDialog("del_dialog");
      },
      del_dialog_okbtn_click: function () {
        this.pageview.hideDialog("del_dialog");
        var _this = this;
        var id = this.deleteObj.datasource.id;
        this.pageview.showSubmiting({
          text: "正在删除",
          successText: null
        });
        this.pageview.ajax({
              // this.pageview.pageManager.appConfig.taskHost + "/taskDetail/del?taskId=" + this.taskid + "&"
          url: "/template/delete/"+id,
          type: "GET",
          data: {

          },
          success: function (data) {
            if (data.code === 0) {
              _this.deleteObj.rowInstance.$el.children().css({width:0, opacity: 0, margin: 0});
              _this.deleteObj.rowInstance.$el.css({margin: 0});
              setTimeout(function(){_this.deleteObj.rowInstance.remove();},500);
            }
            _this.pageview.hideSubmiting(data.code === 0);
          },
          error: function () {
            _this.pageview.hideSubmiting(false);
          }
        });
      },
      stop_icon_click:function(sender,params){
          //停用
          var _this = this;
          var id = sender.datasource.id;
          var rowdata = sender.datasource;
          this.pageview.showSubmiting({successText:null,text:"正在处理中..."});
          this.pageview.ajax({
            url:"/template/stop",
            type:"POST",
            timeout: 7000,
            data:{id:id},
            success:function(data){
              //成功后操作
              _this.pageview.hideSubmiting(data.code === 0);
              rowdata.dataStatus = 0;
              sender.rowInstance.rebind(rowdata);
            },
            error:function(){
              _this.pageview.hideSubmiting(false);
            }
          });
      },
      start_icon_click:function(sender,params){
          //启用
        var _this = this;
        var id = sender.datasource.id;
        var rowdata = sender.datasource;
        this.pageview.showSubmiting({successText:null,text:"正在处理中..."});
        this.pageview.ajax({
          url:"/template/start",
          type:"POST",
          timeout: 7000,
          data:{id:id},
          success:function(data){
            //成功后操作
            _this.pageview.hideSubmiting(data.code === 0);
            rowdata.dataStatus = 1;
            sender.rowInstance.rebind(rowdata);
          },
          error:function(){
            _this.pageview.hideSubmiting(false);
          }
        });
      },
      read_icon_click:function(sender,params){
          //查看
          console.log(sender.datasource);
        this.pageview.go("index/template/designer",{id:sender.datasource.id,status:1});
      },
      edit_icon_click:function(sender,params){
          //编辑
        this.pageview.go("index/template/designer",{id:sender.datasource.id,status:0});
      },
      paginator_change:function(sender,params){
        this._loadData(params.pageIndex);
      }
    };
    return pageLogic;
});
